package com.gr.smartedubackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gr.smartedubackend.common.BaseResponse;
import com.gr.smartedubackend.common.DeleteRequest;
import com.gr.smartedubackend.common.ErrorCode;
import com.gr.smartedubackend.model.dto.notice.NoticeAddRequest;
import com.gr.smartedubackend.model.dto.notice.NoticeQueryRequest;
import com.gr.smartedubackend.model.dto.notice.NoticeUpdateRequest;
import com.gr.smartedubackend.model.entity.Notice;
import com.gr.smartedubackend.model.entity.User;
import com.gr.smartedubackend.service.NoticeService;
import com.gr.smartedubackend.service.UserService;
import com.gr.smartedubackend.utils.ResultUtils;
import com.gr.smartedubackend.utils.ThrowUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    @Resource
    private UserService userService;

    @PostMapping("/add")
    public BaseResponse<Long> addNotice(@RequestBody NoticeAddRequest noticeAddRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(noticeAddRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddRequest, notice);
        notice.setPublisherId(loginUser.getId());
        notice.setPublisherName(loginUser.getUserName());
        boolean result = noticeService.save(notice);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(notice.getId());
    }

    @GetMapping("/get")
    public BaseResponse<Notice> getNoticeById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        Notice notice = noticeService.getById(id);
        ThrowUtils.throwIf(notice == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(notice);
    }

    @PostMapping("/list/page")
    public BaseResponse<Page<Notice>> listNoticeByPage(@RequestBody NoticeQueryRequest noticeQueryRequest) {
        ThrowUtils.throwIf(noticeQueryRequest == null, ErrorCode.PARAMS_ERROR);
        int current = noticeQueryRequest.getCurrent();
        int pageSize = noticeQueryRequest.getPageSize();
        Page<Notice> noticePage = noticeService.page(new Page<>(current, pageSize),
                noticeService.getQueryWrapper(noticeQueryRequest));
        return ResultUtils.success(noticePage);
    }

    @PostMapping("/list")
    public BaseResponse<List<Notice>> listNotice(@RequestBody NoticeQueryRequest noticeQueryRequest) {
        ThrowUtils.throwIf(noticeQueryRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(noticeService.list(noticeService.getQueryWrapper(noticeQueryRequest)));
    }


    @PostMapping("/update")
    public BaseResponse<Boolean> updateNotice(@RequestBody NoticeUpdateRequest noticeUpdateRequest) {
        ThrowUtils.throwIf(noticeUpdateRequest == null || noticeUpdateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateRequest, notice);
        boolean result = noticeService.updateById(notice);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteNotice(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        boolean result = noticeService.removeById(deleteRequest.getId());
        return ResultUtils.success(result);
    }
}
